package com.fly.user.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.fly.user.component.annotion.UserIdAnnotion;
import com.fly.common.common.ErrorConstant;
import com.fly.common.exception.BusinessException;
import com.fly.common.request.user.*;
import com.fly.common.vo.ResponseVO;
import com.fly.common.vo.order.UserOrderVO;
import com.fly.common.vo.user.UserVO;
import com.fly.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @description 用户控制器
 * @author: fly
 * @date: 2018/11/20 14:31
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(value = "User Api", tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     *
     * @param request 用户信息
     * @return 注册结果
     */
    @GetMapping(path = "/register")
    @PreAuthorize(value = "hasRole('ROLE_CLIENT')")
    @ApiOperation(value = "用户注册", notes = "用户注册")
    public ResponseVO register(@Valid AddUserRequest request) {
        ResponseVO responseVO = new ResponseVO();
        try {
            userService.register(request);
            responseVO.setResult(ErrorConstant.SUCCESS, ErrorConstant.SUCCESS_MSG);
        } catch (BusinessException b) {
            log.error("UserController register request -> {}", request.toString(), b);
            responseVO.setResult(b.getErrorCode(), b.getMessage());
        } catch (Exception e) {
            log.error("UserController register request -> {}", request.toString(), e);
            responseVO.setResult(ErrorConstant.METHOD_EXCEPTION, ErrorConstant.METHOD_EXCEPTION_MSG);
        }
        return responseVO;
    }

    /**
     * 根据用户主键获取用户信息
     *
     * @param userId 用户主键
     * @return 用户信息
     */
    @ApiOperation(value = "根据用户主键获取用户信息", notes = "根据用户主键获取用户信息")
    @GetMapping(path = "/queryUserById/{userId}")
    @PreAuthorize(value = "hasRole('ROLE_USER')")
    public ResponseVO<UserVO> queryUserById(@PathVariable Long userId) {
        ResponseVO<UserVO> responseVO = new ResponseVO<>();
        try {
            UserVO userVO = userService.queryUserById(userId);
            responseVO.setResult(ErrorConstant.SUCCESS, ErrorConstant.SUCCESS_MSG, userVO);
        } catch (BusinessException b) {
            log.error("UserController queryUserById userId -> {} \n BusinessException", userId, b.getMessage());
            responseVO.setResult(b.getErrorCode(), b.getMessage());
        } catch (Exception e) {
            log.error("UserController queryUserById userId -> {} Exception", userId, e);
            responseVO.setResult(ErrorConstant.METHOD_EXCEPTION, ErrorConstant.METHOD_EXCEPTION_MSG);
        }
        return responseVO;
    }

    /**
     * 获取用户列表
     *
     * @return 用户列表
     */
    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @GetMapping(path = "/queryUserList")
    @PreAuthorize(value = "hasRole('ROLE_USER')")
    public ResponseVO<List<UserVO>> queryUserList() {
        ResponseVO<List<UserVO>> responseVO = new ResponseVO<>();
        try {
            List<UserVO> userVOS = userService.queryUserList();
            responseVO.setResult(ErrorConstant.SUCCESS, ErrorConstant.SUCCESS_MSG, userVOS);
        } catch (Exception e) {
            e.printStackTrace();
            responseVO.setResult(ErrorConstant.METHOD_EXCEPTION, ErrorConstant.METHOD_EXCEPTION_MSG);
        }
        return responseVO;
    }

    /**
     * 分页获取用户对象
     *
     * @param request 分页信息
     * @return 用户信息
     */
    @ApiOperation(value = "分页获取用户对象", notes = "分页获取用户对象")
    @GetMapping(path = "/queryUserPage")
    public ResponseVO<Page<UserVO>> queryUserPage(QueryUserPageRequest request) {
        ResponseVO<Page<UserVO>> responseVO = new ResponseVO<>();
        try {
            Page<UserVO> userVOPage = userService.queryUserPage(request);
            responseVO.setResult(ErrorConstant.SUCCESS, ErrorConstant.SUCCESS_MSG, userVOPage);
        } catch (Exception e) {
            e.printStackTrace();
            responseVO.setResult(ErrorConstant.METHOD_EXCEPTION, ErrorConstant.METHOD_EXCEPTION_MSG);
        }
        return responseVO;
    }

    /**
     * 新增用户
     *
     * @param request 用户信息
     * @return 新增结果
     */
    @ApiOperation(value = "新增用户", notes = "新增用户")
    @PostMapping(path = "/addUser")
    public ResponseVO addUser(@RequestBody @Valid AddUserRequest request) {
        ResponseVO responseVO = new ResponseVO();
        userService.addUser(request);
        return responseVO.setResult(ErrorConstant.SUCCESS, ErrorConstant.SUCCESS_MSG);
    }

    /**
     * 修改用户
     *
     * @param request 用户信息
     * @return 修改用户结果
     */
    @ApiOperation(value = "修改用户", notes = "修改用户")
    @PutMapping(path = "/updateUser")
    public ResponseVO updateUser(@RequestBody @Valid UpdateUserRequest request) {
        return userService.updateUser(request);
    }

    /**
     * 删除用户信息
     *
     * @param userId 用户主键
     * @return 删除结果
     */
    @ApiOperation(value = "删除用户信息", notes = "删除用户信息")
    @DeleteMapping(path = "/deleteUser/{userId}")
    public ResponseVO deleteUser(@PathVariable Long userId) {
        userService.deleteUser(userId);
        return new ResponseVO();
    }

    /**
     * 批量保存-异常回滚
     *
     * @return 批量保存结果
     */
    @ApiOperation(value = "批量保存-异常回滚", notes = "批量保存-异常回滚")
    @PostMapping(path = "/batchSaveUser")
    public ResponseVO batchSaveUser() {
        ResponseVO responseVO = new ResponseVO();
        try {
            userService.batchSaveUser();
            responseVO.setResult(ErrorConstant.SUCCESS, ErrorConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.error("UserController batchSaveUser ExceptionMsg -> {}", e.getMessage(), e);
            responseVO.setResult(ErrorConstant.METHOD_EXCEPTION, e.getMessage());
        }
        return responseVO;
    }

    /**
     * 获取用户数量
     *
     * @return 用户数量
     */
    @ApiOperation(value = "获取用户数量", notes = "获取用户数量")
    @GetMapping(path = "/queryUserCount")
    public ResponseVO<Long> queryUserCount() {
        ResponseVO<Long> responseVO = new ResponseVO<>();
        Long userCount = userService.queryUserCount();
        responseVO.setResult(ErrorConstant.SUCCESS, ErrorConstant.SUCCESS_MSG, userCount);
        return responseVO;
    }

    /**
     * 根据用户主键获取用户信息
     *
     * @param userId 用户主键
     * @return 用户信息
     */
    @ApiOperation(value = "根据用户主键获取用户信息", notes = "根据用户主键获取用户信息")
    @GetMapping(path = "/findById/{userId}")
    public ResponseVO<UserVO> findById(@PathVariable Long userId) {
        ResponseVO<UserVO> responseVO = new ResponseVO<>();
        try {
            UserVO userVO = userService.findById(userId);
            responseVO.setResult(ErrorConstant.SUCCESS, ErrorConstant.SUCCESS_MSG, userVO);
        } catch (Exception e) {
            log.error("UserController findById userId -> {} Exception", userId, e);
            responseVO.setResult(ErrorConstant.METHOD_EXCEPTION, ErrorConstant.METHOD_EXCEPTION_MSG);
        }
        return responseVO;
    }

    /**
     * 获取我的订单
     *
     * @param request 用户主键
     * @return 订单信息
     */
    @ApiOperation(value = "获取我的订单", notes = "获取我的订单")
    @GetMapping(path = "/queryOrderByUserId")
    @PreAuthorize(value = "hasRole('ROLE_USER')")
    @UserIdAnnotion
    public ResponseVO<List<UserOrderVO>> queryOrderByUserId(UserRequest request) {
        ResponseVO<List<UserOrderVO>> responseVO = new ResponseVO<>();
        List<UserOrderVO> userOrderVOS = userService.queryMyOrder(request.getUserId());
        responseVO.setResult(ErrorConstant.SUCCESS, ErrorConstant.SUCCESS_MSG, userOrderVOS);
        return responseVO;
    }
}